package cc.linker.steplink.crm.merchanth5.controller;


import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.constant.OssConstant;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.SystemProperties;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.ZsContentService;
import cc.linker.steplink.crm.merchanth5.service.ZsGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/h5/zscontent")
public class ZsContentController {

    @Autowired
    private ZsContentService zsContentService;

    @Autowired
    private HttpService httpService;

    @Autowired
    private SystemProperties systemProperties;

    @Autowired
    private RequestFilter requestFilter;

    @Autowired
    private ZsGoodsService zsGoodsService;

    /**
     * @Description: 获取货架商城列表
     * @Param: goodsId 内容的商品id
     * @Return: java.util.Map<java.lang.String, java.lang.Object>
     * @Author: ch
     * @Date: 2019/12/5 15:02
     * @Version: 1.11.10
     **/
    @PostMapping("/getContentInof")
    public Map<String, Object> getContentInof(@RequestBody Map map, HttpServletRequest request) {
        if (map.get("goodsId") == null) {
            throw GlobalException.build("参数错误");
        }
        String goodsId = map.get("goodsId").toString();
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String shareCustomerId = "";
        if(!StringUtils.isEmpty(map.get("shareCustomerId"))){
            shareCustomerId = map.get("shareCustomerId").toString();
        }
        int shareType = 1;
        if(!StringUtils.isEmpty(map.get("shareType"))){
            shareType = Integer.parseInt(map.get("shareType").toString());
        }
        zsGoodsService.setGoodsShareLog(Long.parseLong(goodsId),shareCustomerId,shareType,customerId);
        return zsContentService.getContentInof(goodsId, customerId);
    }

    /**
     * 获取积分兑换内容详情
     * @param map
     * @param request
     * @return
     */
    @PostMapping("/getIntegralContentInfo")
    public Map<String, Object> getIntegralContentInfo(@RequestBody Map map, HttpServletRequest request) {
        if (map.get("goodsId") == null) {
            throw GlobalException.build("参数错误");
        }
        String goodsId = map.get("goodsId").toString();
        Map<String, Object> integralGoodsInfo = zsContentService.getIntegralGoodsInfo(goodsId);
        if(null == integralGoodsInfo){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String shareCustomerId = "";
        if(!StringUtils.isEmpty(map.get("shareCustomerId"))){
            shareCustomerId = map.get("shareCustomerId").toString();
        }
        int shareType = 1;
        if(!StringUtils.isEmpty(map.get("shareType"))){
            shareType = Integer.parseInt(map.get("shareType").toString());
        }
        zsGoodsService.setGoodsShareLog(Long.parseLong(integralGoodsInfo.get("originalGoodsId").toString()),shareCustomerId,shareType,customerId);
        Map<String, Object> goodsInfo = zsContentService.getContentInof(integralGoodsInfo.get("originalGoodsId").toString(), customerId);
        goodsInfo.put("shareTitle",integralGoodsInfo.get("shareInfo"));
        goodsInfo.put("shareInfo",integralGoodsInfo.get("shareInfo"));
        goodsInfo.put("belongId",integralGoodsInfo.get("belongId"));
        goodsInfo.put("moneyAmount",integralGoodsInfo.get("moneyAmount"));
        goodsInfo.put("integral",integralGoodsInfo.get("integral"));
        goodsInfo.put("quotaNum",integralGoodsInfo.get("quotaNum"));
        goodsInfo.put("quotaLevel",integralGoodsInfo.get("quotaLevel"));
        goodsInfo.put("goodsCount",integralGoodsInfo.get("goodsCount"));
        goodsInfo.put("originalCost",integralGoodsInfo.get("originalCost"));
        goodsInfo.put("integralGoodsId",map.get("goodsId"));
        if(!StringUtils.isEmpty(integralGoodsInfo.get("quotaVips"))){
            List<Map> vipList = zsContentService.getQuotaVipList(integralGoodsInfo.get("quotaVips").toString(),customerId);
            goodsInfo.put("quotaVipList",vipList);
        }
        return goodsInfo;
    }

    /**
     * @Description: 获取素材详情
     * @Param: ResId 素材ID
     * @Return: java.lang.Object
     * @Author: ch
     * @Date: 2019/12/10 16:51
     * @Version: 1.11.0
     **/
    @PostMapping("/getResourceInfo")
    public Object getResourceInfo(@RequestBody Map<String, Object> params) {
        String url = systemProperties.getMaterialCenter() + OssConstant.OSS_INFO;
        // 请求oss结果
        String result = "";
        try {
            result = httpService.doPost(url, JSON.toJSONString(params), OssConstant.JSON);
        } catch (Exception e) {
            LOGGER.error("素材中心调用异常", e);
            throw GlobalException.build("素材中心调用异常");
        }
        return result;
    }

    /**
     * 保存内容播放进度
     *
     * @param contentId
     * @param schedule
     * @param request
     */
    @GetMapping("/saveSchedule")
    public void saveSchedule(String contentId, String schedule, HttpServletRequest request) {
        if (StringUtils.isEmpty(contentId) || StringUtils.isEmpty(schedule)) {
            throw GlobalException.build("缺少参数");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        zsContentService.saveSchedule(contentId, schedule, customerId);
    }

    /**
     * 记录播放次数
     *
     * @param contentId
     * @param request
     */
    @GetMapping("/play")
    public void play(String contentId, HttpServletRequest request) {
        if (StringUtils.isEmpty(contentId)) {
            throw GlobalException.build("缺少参数");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        zsContentService.play(contentId, customerId);
    }
}
